
//设置html根目录字体大小
require(['jquery','../modules/pageSize'],function($,pageSetSize){
    setRootFontSize(pageSetSize);
})

require(['jquery','swiper','../modules/popup'],function($,swiper,popup){
    var isEnd =  false, isAjax = false , isAddCart = false;
    var likeShopNumber = '',likeStoreNumber = '';
    //加载默认local_id
    myLoveGoodsGet();
    function myLoveTable(e){
        e.preventDefault();
        var _self = $(this);
        _self.toggleClass('active');
        _self.siblings().removeClass('active');
        if(_self.index() == 0){
            myLoveGoodsGet();
        }
        else{
            myLoveStoreGet();
        }
    }
    function myLoveGoodsGet(){
        $('.mlLove-goods-list').html('');
        $('.modify-bar').show();
        $('.modify').show();
        $('.complete').hide();
        if(isEnd == true || isAjax == true){
            return false;
        }
        var _goodsHtml = '';
        var url = '/Home/Member/memberMyLikeGood'+getFixedExtraUrl();
        var data ={type:1};
        isAjax =  true;
        $.ajax({
            url:url,
            type:"get",
            data:data,
            dataType:'json',
            beforeSend: function () {
                $("body").append('<div id="pload" style="position:fixed;top:0;z-index:1200;background: rgba(0,0,0,0.3);width:100%;height:100%;margin:auto auto;">' +
                    '<p style="width:2.0rem;height: 2.0rem; margin: 0 auto;text-align: center; position: relative; top: 5rem;">' +
                    '<span style="font-size: .32rem;color: #3c6a63;">加载中...</span>' +
                    '<img src="/Public/Home/images/loading-more.gif" style="display: block;width: .32rem;height: .32rem; margin: 0 auto;"></p>'+
                    '</div>');
            },
            success:function(data){
                //console.log(data);
                isAjax =  false;
                var _goodsList = data.list;
                var failure = data.failure; // 失效商品个数
                var _goodsImg = data.image;
                var isxq = '';
                likeShopNumber = data.goodLenth;  // 关注的商品个数
                likeStoreNumber = data.storeLenth;  // 关注的店铺个数
                $(".modify-bar span i").text(failure);
                if (_goodsList) {
                    for (var i = 0; i < _goodsList.length; i++) {
                        var _goodsImgUrl = '';
                        var goodsDes = _goodsList[i].goods_id;
                        if (_goodsImg) {
                            _goodsImgUrl = _goodsImg[goodsDes];
                        }
                        var goodsRet = _goodsList[i];
                        var store_status = goodsRet.store_status; // 失效状态

                        if (store_status == 'false' || !store_status) {
                            _goodsHtml += '<div class="single-item failure-item">';
                        }
                        else {
                            _goodsHtml += '<div class="single-item dataItem">';
                        }
                        if (store_status == 'false' || !store_status) {
                            _goodsHtml += '<div class="swipe-item">';
                        } else {
                            _goodsHtml += '<div class="swipe-item swp-right-slide">';
                        }

                        if(_goodsList[i]['store']-_goodsList[i]['freez']<=0){
                            isxq = 'sold_out';
                        }


                        _goodsHtml += '<a href="/Goods/detail/id/' + goodsRet.store_pid + '' + getFixedExtraUrl() + '" class="img '+  isxq +'"' +'><img src="'+getDefaultImg(_goodsImgUrl,true)+'" alt=""></a>';
                        _goodsHtml += '<div class="goods-text">';
                        _goodsHtml += '<a href="/Goods/detail/id/' + goodsRet.store_pid + '' + getFixedExtraUrl() + '" class="name">';
                        _goodsHtml += '<h3>' + goodsRet.p_name + '</h3>';
                        _goodsHtml += '<span><i></i>' + goodsRet.local_name + '</span>';
                        if (store_status == 'false' || !store_status) {
                            _goodsHtml += '<em>失效</em>';
                        }
                        _goodsHtml += '</a>';
                        if (store_status == 'false' || !store_status) {
                            _goodsHtml += '<p class="seek-similar-shop" data-id="' + goodsRet.like_id + '"><button>清除</button></p>';
                        }
                        else {
                            _goodsHtml += '<p class="price"  value="¥' + goodsRet.price + '">¥' + goodsRet.price + '</p>';
                        }
                        _goodsHtml += '</div>';
                        _goodsHtml += '</div>';
                        if (store_status != 'false') {
                            _goodsHtml += '<div class="static-item">';
                            _goodsHtml += '<div class="choice-btn" data-id="' + goodsRet.like_id + '"></div>';
                            _goodsHtml += '<a href="javascript:void(0);" class="delete" data-id="' + goodsRet.like_id + '">取消关注</a>';
                            _goodsHtml += '<a href="javascript:void(0);" class="collect" data-sid ="'+goodsRet.local_id+'" data-pid ="'+goodsRet.product_id+'" data-gid ="'+goodsRet.goods_id+'" data-spid ="'+goodsRet.store_pid+'">移至购物车</a>';
                            _goodsHtml += '</div>';
                        }
                        _goodsHtml += '</div>';
                        if (store_status == 'false' || !store_status) {
                            $(".mylove-content").css({"padding-bottom":"1.36rem"});
                            $(".delete-shop-failure").show();
                        }
                    }
                }
                if(likeShopNumber == 0){
                    $(".empty-box").show();
                    $('.empty-state').text('商品');
                    isAjax = true;
                }
                else{
                    $(".empty-box").hide();
                    $('.empty-state').text('');
                    isAjax = false;
                }
                $('.tab-title a:eq(0) span em').html(likeShopNumber);
                $('.tab-title a:eq(1) span em').html(likeStoreNumber);
                $('.mlLove-goods-list').html(_goodsHtml);
                operateGoodsDemo();
            },
            complete: function () {
                $("#pload").remove();
            },
            error:function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })
    }
    function myLoveStoreGet(){
        isAjax = false;
        if(isEnd == true || isAjax == true){
            return false;
        }
        if(likeStoreNumber == 0){
            $(".empty-box").show();
            $('.empty-state').text('店铺');
            isAjax = true;
        }
        else{
            $(".empty-box").hide();
            $('.empty-state').text('');
            isAjax = false;
            $('.mlLove-goods-list').html('');
            $('.modify-bar').hide();
            $('.fixed-bot-modify').hide();
            $('.delete-shop-failure').hide();
        }
        var _storeHtml = '';
        var url = '/Home/Member/memberMyLikeStore'+getFixedExtraUrl();
        var data ={type:1};
        isAjax =  true;
        $.ajax({
            url:url,
            type:"get",
            data:data,
            dataType:'json',
            beforeSend: function () {
                $("body").append('<div id="pload" style="position:fixed;top:0;z-index:1200;background: rgba(0,0,0,0.3);width:100%;height:100%;margin:auto auto;">' +
                    '<p style="width:2.0rem;height: 2.0rem; margin: 0 auto;text-align: center; position: relative; top: 5rem;">' +
                    '<span style="font-size: .32rem;color: #3c6a63;">加载中...</span>' +
                    '<img src="/Public/Home/images/loading-more.gif" style="display: block;width: .32rem;height: .32rem; margin: 0 auto;"></p>'+
                    '</div>');
            },
            success:function(data){
                //console.log(data);
                isAjax =  false;
                var storeList = data.list;
                if(storeList){
                    for(var i = 0; i<storeList.length; i++){
                        _storeHtml +='<div class="moLove-shop-list">';
                        _storeHtml +='<div class="shop-name">';
                        _storeHtml +='<a href="/Store/index/'+storeList[i].local_id+''+getFixedExtraUrl()+'">';
                        _storeHtml +='<i><img src="'+getDefaultImg(storeList[i].local_log,true)+'"></i>';
                        _storeHtml +='<div class="shop-message">';
                        _storeHtml +='<h3>'+storeList[i].local_name+'</h3>';
                        _storeHtml +='<span>'+storeList[i].fans+'粉丝</span>';
                        _storeHtml +='</div>';
                        _storeHtml +='</a>';
                        _storeHtml +='</div>';
                        _storeHtml +='<div class="delete-shopAttention" data-id="'+storeList[i].like_id+'">取消关注</div>';
                        _storeHtml +='</div>';
                    }
                }
                $('.mlLove-goods-list').html(_storeHtml);
                operateStoreDemo()
            },
            complete: function () {
                $("#pload").remove();
            },
            error:function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })

    }
    $(".tab-title").on("click","a",myLoveTable);

    function operateGoodsDemo(){
        //编辑状态切换
        var $swipeShop = $('.swipe-item');
        var isChickEvent = false;
        if(!$swipeShop.parent().hasClass("failure-item")){
            $('.delete-shop-failure a').remove();
        }
        $('.modify-bar a').on('click',function(){
            isChickEvent = true;
            $(this).hide().siblings('a').show();
            if($(this).hasClass('modify')){
                $('.swp-right-slide').animate({'left':40},200);
                $('.fixed-bot-modify').show();
                $('.all-delete').removeClass('active');
            }
            else{
                $('.swp-right-slide').animate({'left':0},200);
                $('.fixed-bot-modify').hide();
                isChickEvent = false;
            }
        });
        // 滑动
        $swipeShop.bind('swipeleft',function(){
            if(isChickEvent)
                return;
            if($(this).parent().hasClass('failure-item')){
                return;
            }
            else {
                $(this).animate({'left':-110},200);
            }
        });
        $swipeShop.bind('swiperight',function(){
            if($(this).parent().hasClass('failure-item')){
                return;
            }
            else {
                $(this).animate({'left':0},200);
            }
        });
        // 我喜欢中的商品 移至购物车
        $('.mlLove-goods-list .collect').each(function(){
            //var href = $(this);
            $(this).on('click',function(e){
                var goodsInfo = {
                    pid : $(this).data('pid'),
                    gid : $(this).data('gid'),
                    sid : $(this).data('sid'),
                    spid : $(this).data('spid')
                };
                if(isAddCart){
                    return false;
                }
                e.stopPropagation();
                isAddCart = true;
                $.ajax({
                    url:"/Home/carts/addCart"+getFixedExtraUrl(),
                    type:"post",
                    data: {
                        'pid': goodsInfo.pid,
                        'buyNum': 1,
                        'goods_id': goodsInfo.gid,
                        'local_id': goodsInfo.sid,
                        'spid': goodsInfo.spid
                    },
                    dataType: "json",
                    success:function(str){
                        popup.alertPopup(str.msg);
                        isAddCart = false;
                    },
                    error: function(){
                        isAddCart = false;
                        popup.alertPopup("服务器异常,请稍后重试！");
                    }
                })
            })
        });

        // 取消商品关注
        $('.mlLove-goods-list .delete').each(function(){
            //var href = $(this);
            $(this).on('click',function(e){
                var id = $(this).data('id');
                e.stopPropagation();
                var currParent =$(this).parents(".single-item");
                popup.popup('.deleteGoods');
                $('.deleteGoods .confirm').on('click',function(){
                    ajaxAttention(id,'1',currParent,"/Home/Member/cancelLike");
                })
            })
        });
        // 删除失效商品
        $('.delete-shop-failure a').click(function(){
            var id_cur = [];
            $('.seek-similar-shop').each(function(){
                id_cur.push($(this).data('id'));
            });
            //console.log(id_arr);return;
            $.ajax({
                url:"/Home/Member/deleteLike"+getFixedExtraUrl(),
                type:"post",
                data:{id:id_cur,type:1},
                dataType: "json",
                success:function(str){
                    popup.alertPopup(str.msg);
                    if(str.stat){
                        $('.failure-item').remove();
                        $(".delete-shop-failure").hide();
                    }
                },
                error: function(){
                    popup.alertPopup("服务器异常,请稍后重试！");
                }
            })
        });
        $('.failure-item').each(function(){
            $(this).find('.seek-similar-shop').click(function(){
                var _this = $(this);
                var id = $(this).data('id');
                $.ajax({
                    url:"/Home/Member/deleteLike"+getFixedExtraUrl(),
                    type:"post",
                    data:{id:id,type:1},
                    dataType: "json",
                    success:function(str){
                        popup.alertPopup(str.msg);
                        if(str.stat){
                            _this.parents('.failure-item').remove();
                        }
                    },
                    error: function(){
                        popup.alertPopup("服务器异常,请稍后重试！");
                    }
                })
            })
        });
        // 选择按钮删除
        var choiceLen = 0;
        var defaultLen = $('.choice-btn').length;
        $('.choice-btn').each(function(){
            $(this).on('click',function(){
                $(this).toggleClass('active');
                if($(this).hasClass('active')){
                    choiceLen++;
                }
                else{
                    choiceLen--;
                }
                if(choiceLen==defaultLen){
                    $('.all-delete').addClass('active');
                }
                else{
                    $('.all-delete').removeClass('active');
                }
            })
        });
        $('.all-delete').on('click',function(){
            if($(this).hasClass('active')){
                $(this).removeClass('active');
                $('.choice-btn').removeClass('active');
            }
            else{
                $(this).addClass('active');
                $('.choice-btn').addClass('active');
            }
        });
        $('.fixed-bot-modify .delete-btn').on('click',function(){
            popup.popup('.delete-Goods');
            $('.delete-Goods .confirm').on('click',function(){
                var id_arr = [];
                $('.choice-btn').each(function(){
                    id_arr.push($(this).data('id'));
                });
                //console.log(id_arr);return;
                $.ajax({
                    url:"/Home/Member/deleteLike"+getFixedExtraUrl(),
                    type:"post",
                    data:{id:id_arr,type:1},
                    dataType: "json",
                    success:function(str){
                        popup.alertPopup(str.msg);
                        if(str.stat){
                            $('.choice-btn').each(function(){
                                if($(this).hasClass('active')){
                                    $(this).parents('.single-item').remove();
                                    $('.tab-title a:eq(0) span em').html(likeShopNumber - id_arr.length);
                                    setTimeout(function(){
                                        if($('.tab-title a:eq(0) span em').text() == 0){
                                            $(".empty-box").show();
                                            $('.empty-state').text('商品');
                                        }
                                    },1500)
                                }
                                $('.all-delete').removeClass('active');
                            })
                        }
                    },
                    error: function(){
                        popup.alertPopup("服务器异常,请稍后重试！");
                    }
                })

            })
        })
    }

    function operateStoreDemo(){
        // 取消店铺关注
        $('.moLove-shop-list .delete-shopAttention').each(function(){
            //var href = $(this);
            $(this).on('click',function(e){
                var id = $(this).data('id');
                e.stopPropagation();
                var currParent =$(this).parent();
                popup.popup('.deleteShops');
                $('.deleteShops .confirm').on('click',function(){
                    ajaxAttention(id,'0',currParent,"/Home/Member/cancelLike");
                })
            })
        });
    }


    function ajaxAttention(id,type,currParent,url){
        var data={id:id,type:type};
        //console.log(data);return;
        $.ajax({
            url:url,
            type:"post",
            data:data,
            dataType: "json",
            success:function(str){
                //console.log(str);return;
                popup.alertPopup(str.msg);
                if(str.stat){
                    currParent.remove();
                    if(type ==0){
                        $('.tab-title a:eq(1) span em').html(likeStoreNumber - 1);
                        setTimeout(function(){
                            if($('.tab-title a:eq(1) span em').text() == 0){
                                $(".empty-box").show();
                                $('.empty-state').text('店铺');
                            }
                        },1500)
                    }
                    if(type ==1){
                        $('.tab-title a:eq(0) span em').html(likeShopNumber - 1);
                        setTimeout(function(){
                            if($('.tab-title a:eq(0) span em').text() == 0){
                                $(".empty-box").show();
                                $('.empty-state').text('商品');
                            }
                        },1500)
                    }

                }
            },
            error: function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })
    }
});
